<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      size="small"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="仓库名称"></el-input>
      </el-form-item>

      <el-form-item
        label="仓库物理名称"
        v-if="physicalNameShow"
        prop="physicalName"
      >
        <el-input
          v-model="dataForm.physicalName"
          :disabled="physicalNameDisabled"
          placeholder="仓库物理名称"
        ></el-input>
      </el-form-item>

      <el-form-item label="仓库编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="仓库编码"></el-input>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
      </el-form-item>

      <el-form-item label="排序号" prop="orderNum">
        <el-input-number
          v-model="dataForm.orderNum"
          controls-position="right"
          :min="0"
          label="排序号"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="仓库状态" size="mini">
        <el-radio-group v-model="dataForm.dataState">
          <el-radio :label="0">正常</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>


<script>
import { saveOrUpdateDb } from "@/api/table";
export default {
  data() {
    return {
      visible: false,
      physicalNameShow: false,
      physicalNameDisabled: false,
      dataForm: {
        id: "",
        code: "",
        parentId: "",
        physicalName: "",
        name: "",
        remark: "",
        orderNum: 0,
        dataState: 0,
      },
      dataRule: {
        name: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" },
        ],
        code: [
          { required: true, message: "仓库编码不能为空", trigger: "blur" },
        ],
      },
      dataListLoading: "",
      dictList: [],
      dictListTreeProps: {
        label: "name",
        children: "children",
      },
    };
  },
  components: {},
  activated() {},
  methods: {
    init(data) {
      this.dataForm.id = 0;

      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
      });
      if (data.addChildren == 1) {
        this.physicalNameShow = false;
        this.physicalNameDisabled = false;
        // 添加子节点
        this.dataForm.parentId = data.id;
      } else if (data.addChildren == 2) {
        this.physicalNameShow = true;
        this.physicalNameDisabled = true;
        // 修改
        this.dataForm.id = data.id;
        this.dataForm.name = data.name;
        this.dataForm.parentId = data.parentId;
        this.dataForm.url = data.url;
        this.dataForm.physicalName = data.physicalName;

        this.dataForm.orderNum = data.orderNum;
        this.dataForm.remark = data.remark;
        this.dataForm.dataState = data.dataState;
        this.dataForm.code = data.code;
      }
    },
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          saveOrUpdateDb({
            id: this.dataForm.id || "",
            parentId: this.dataForm.parentId || 0,
            name: this.dataForm.name,
            code: this.dataForm.code,
            remark: this.dataForm.remark,
            orderNum: this.dataForm.orderNum,
            dataState: this.dataForm.dataState,
          }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>
